#include "webser.hpp"

IPAddress addr(192, 168, 4, 1);
WebServer server(80);

String getContentType(String filename)
{
    if (server.hasArg("download"))
        return "application/octet-stream";
    else if (filename.endsWith(".htm"))
        return "text/html";
    else if (filename.endsWith(".html"))
        return "text/html";
    else if (filename.endsWith(".css"))
        return "text/css";
    else if (filename.endsWith(".js"))
        return "application/javascript";
    else if (filename.endsWith(".png"))
        return "image/png";
    else if (filename.endsWith(".gif"))
        return "image/gif";
    else if (filename.endsWith(".jpg"))
        return "image/jpeg";
    else if (filename.endsWith(".ico"))
        return "image/x-icon";
    else if (filename.endsWith(".xml"))
        return "text/xml";
    else if (filename.endsWith(".pdf"))
        return "application/x-pdf";
    else if (filename.endsWith(".zip"))
        return "application/x-zip";
    else if (filename.endsWith(".gz"))
        return "application/x-gzip";
    return "text/plain";
}

/**/
void sendwifistate();
void acceptwifidata();
void diswificonnect();
void sendshowstate();
void changeshowstate();
void onNotFound();
//****************************//

bool handleFileRead(String path)
{
    Serial.println("handleFileRead: " + path);
    if (path.endsWith("/"))
    {
        path += "index.html";
    }
    String contentType = getContentType(path);

    if (SPIFFS.exists(path))
    {
        File file = SPIFFS.open(path, "r");
        server.streamFile(file, contentType);
        file.close();
        return true;
    }
    return false;
}

void webServerInit()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_MODE_APSTA);

    WiFi.softAP("ESP32智能时钟AP");

    Serial.print("Soft-AP IP address = ");
    Serial.println(WiFi.softAPIP());

    if (MDNS.begin("esp32"))
    {
        Serial.println("MDNS responder started");
    }

    server.on("/getwifistate/", sendwifistate);
    server.on("/sendwifidata/", acceptwifidata);
    server.on("/disconnect/", diswificonnect);
    server.on("/getshowstate/", sendshowstate);
    server.on("/sendshowstate/", changeshowstate);
    server.onNotFound(onNotFound);

    server.begin();
    Serial.println("HTTP server started");
}

/**/
void onNotFound()
{
    if (!handleFileRead(server.uri()))
    {
        server.send(404, "text/plain", "FileNotFound");
    }
}

void sendwifistate()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        server.send(200, "text/plain", WiFi.SSID());
    }
    else
    {
        server.send(200, "text/plain", "");
    }
    Serial.println("client try get wifistate");
}

void acceptwifidata()
{
    /*for(int i=0;i<server.args();i++)
  {
    Serial.print(server.argName(i));
    Serial.println(server.arg(i));
  }*/
    String ssid = server.arg("ssid");
    String pswd = server.arg("pswd");

    if (WiFi.status() == WL_CONNECTED && WiFi.SSID() != ssid)
    {
        WiFi.disconnect();
        while (WiFi.status() == WL_CONNECTED)
            ;
    }

    WiFi.begin(ssid.c_str(), pswd.c_str());
    int count = 0;
    while (WiFi.status() != WL_CONNECTED && ++count < 100)
    {
        delay(100);
        Serial.print('.');
    }
    Serial.println();
    if (WiFi.status() == WL_CONNECTED)
    {
        Serial.println("connect to " + ssid + "success");
        server.send(200, "text/plain", "ture");
        updataTime();

        updataWifidata(ssid, pswd);
    }
    else
    {
        Serial.println("failed connect to " + ssid);
        server.send(200, "text/plain", "false");
    }
}

void diswificonnect()
{
    WiFi.disconnect();
    while (WiFi.status() == WL_CONNECTED)
        ;
    server.send(200, "text/plain", "wifi disconnected");
    Serial.println("disconnected!");
}

void sendshowstate()
{
    if (showstate == 1)
    {
        server.send(200, "text/plain", "on");
    }
    else
    {
        server.send(200, "text/plain", "off");
    }
    Serial.println("client try get showstate");
}

void changeshowstate()
{
    String state = server.arg("state");
    if (state == "on")
    {
        displayState(1);
    }
    else if (state == "off")
    {
        displayState(0);
    }
    Serial.println("client change show state");
}
